package struts.form;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * 创建者 zhuhong  E-mail: zhuhong719@126.com
 * 创建时间：Jul 16, 2014 9:10:49 PM
 * 版本：
 * 类说明：
 * 修改者：
 * 修改时间：Jul 16, 2014 9:10:49 PM
 * 修改说明：
 */
public class Struts_Config {
	public Struts_Config(){
		
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, XmlBean> struts_xml(String filePath) throws Exception{
		SAXBuilder builder =new SAXBuilder();
		Document document=builder.build(new File(filePath));
		Element root=document.getRootElement();
		Map<String, XmlBean> xmlmap=new HashMap<String, XmlBean>();
		Element actionroot=root.getChild("action-mapping");
		List<Element> action=actionroot.getChildren();
		for (Element element : action) {
			XmlBean bean=new XmlBean();
			String name=element.getAttributeValue("name");
			bean.setBeanName(name);
			Element actionform=root.getChild("form-beans");
			List<Element> form=actionform.getChildren();
			for (Element ele : form) {
				if(ele.getAttributeValue("name").equals(name)){
					String formClass=ele.getAttributeValue("class");
					bean.setFormClass(formClass);
					break;
				}
			}
			String path=element.getAttributeValue("path");
			bean.setPath(path);
			String type=element.getAttributeValue("type");
			bean.setActionClass(type);
			List<Element> forward=element.getChildren();
			Map<String, String> map=new HashMap<String, String>();
			for (Element e : forward) {
				String fname=e.getAttributeValue("name");
				String fvalue=e.getAttributeValue("value");
				map.put(fname, fvalue);
			}
			bean.setActionFormward(map);
			xmlmap.put(path, bean);
		}
		return xmlmap;
	}
	
	public static void main(String[] args) {
		try {
			Map<String, XmlBean> map= Struts_Config.struts_xml("src\\struts-config.xml");
			for (String key : map.keySet()) {
				System.out.println(key);
				System.out.println(map.get(key));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
